import { mailAccountMock } from './data/account'
import Mock from 'mockjs'
import { getNowDateTime } from '../../../utils'
let mailAccountList = mailAccountMock(400)
export default [
  {
    url: '/system/mail-account/create', // 新增
    method: 'post',
    response: data => {
      insert(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/mail-account/update', // 修改
    method: 'put',
    response: data => {
      updateById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/mail-account/delete' + '.*'), // 删除
    method: 'delete',
    response: params => {
      deleteById(params)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/mail-account/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  },
  {
    url: RegExp('/system/mail-account/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/system/mail-account/simple-list' + '.*'), // 查询精简列表
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListBySimple()
      }
    }
  }
]

const getListByPage = params => {
  let result = mailAccountList
  result = result.filter(item => !item.deleted)
  if (params.query.mail) {
    result = result.filter(item => item.mail.indexOf(params.query.mail) !== -1)
  }
  if (params.query.username) {
    result = result.filter(item => item.username.indexOf(params.query.username) !== -1)
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListBySimple = () => {
  return mailAccountMock(200)
    .filter(item => !item.deleted)
    .map(item => ({
      id: item.id,
      mail: item.mail,
      username: item.username,
      password: item.password,
      host: item.host,
      port: item.port,
      sslEnable: item.sslEnable
    }))
}

const insert = data => {
  mailAccountList.unshift({
    ...data.body,
    id: Mock.mock('@natural(400, 10000)'),
    creator: '1',
    create_time: getNowDateTime(),
    updater: '',
    update_time: '',
    deleted: false,
    deleted_time: ''
  })
}

const updateById = data => {
  mailAccountList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const deleteById = params => {
  mailAccountList.forEach(item => {
    if (item.id === parseInt(params.query.id)) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}

const getInfoById = params => {
  const result = mailAccountList.filter(item => {
    return item.id === parseInt(params.query.id)
  })
  if (result.length > 0) {
    return result[0]
  } else {
    return null
  }
}
